package org.example.code.test2024.nine_mouth;

import java.util.*;

class RecentCounter {
    Queue<Integer> queue;

    public RecentCounter() {
        queue = new LinkedList<Integer>();
    }
    
    public int ping(int t) {
        queue.add(t);
        while(!queue.isEmpty()&&queue.peek() <= t-3000) {
            System.out.println(queue.peek()+"    "+(t-3000));
            queue.poll();
        }
        return queue.size();
    }


    public int maxTaskAssign(int[] tasks, int[] workers, int pills, int strength) {
        Arrays.sort(tasks);
        Arrays.sort(workers);
       int l=0,r=Math.min(tasks.length,workers.length);
       while(l<r){
           int mid=(l+r+1)/2;
           if(fun(mid,tasks,workers,pills,strength)){
               l=mid;
           }else{
               r=mid-1;
           }
       }
       return l;
    }

    private boolean fun(int x, int[] tasks, int[] workers, int pills, int strength) {
        Deque<Integer> deque = new ArrayDeque<>();
        int sum=0;
        int j=workers.length-1;
        for(int i=x-1;i>=0;i--){
            while(j>=0&&workers[j]+strength>=tasks[i]){
                deque.addLast(workers[j]);
                j--;
            }
            if(deque.isEmpty()){
                return false;
            }
            if(deque.peekFirst()>=tasks[i]){
                deque.removeFirst();
            }else if(sum<pills){
                deque.removeLast();
                sum++;
            }else{
                return false;
            }
        }
        return true;
    }

}
